package com.khaled.protclass.server.bean;

import java.util.ArrayList;

import com.khaled.protclass.model.base.IProtein;
import com.khaled.protclass.model.bean.protein.ProteinType;
import com.khaled.protclass.model.loader.ProteinLoader;
import com.khaled.protclass.model.util.UtilAminoAcids;
import com.khaled.protclass.server.common.Constants;

public class RequestForm
{
	private String proteinsSequence;

	private PredictionApproach predictionApproach;

	private double specificity;

	private String email;

	private ArrayList<IProtein> proteinList;

	private ArrayList<String> errorMessages = new ArrayList<String>();

	public RequestForm(String proteinsSequence, String approach , double specificity, String email)
	{
		this.proteinsSequence = proteinsSequence;

		this.predictionApproach = new PredictionApproach(approach);

		this.specificity = specificity;
		
		this.email = email;
		
		// load and validate protein sequence
		loadProteins();
	}

	public RequestForm()
	{
		// TODO Auto-generated constructor stub
	}

	public ArrayList<String> validate()
	{
		
		return errorMessages;
	}

	private void loadProteins()
	{
		if (this.proteinsSequence == null)
		{
			addErrorMessage(Constants.FORM_VALIDATION_PROTEIN_SEQUENCE_EMPTY);
		}
		else if ("".equals(this.proteinsSequence.trim()))
		{
			addErrorMessage(Constants.FORM_VALIDATION_PROTEIN_SEQUENCE_EMPTY);
		}
		else
		{
			ProteinLoader loader = ProteinLoader.getInstance(ProteinType.getSequenceType());
			this.proteinList = loader.loadProteinsFromString(this.getProteinsSequence(), null);

			if (this.proteinList == null || this.proteinList.size() == 0)
			{
				addErrorMessage(Constants.FORM_VALIDATION_PROTEIN_SEQUENCE_INCORRECT);
			}
			else
			{
				for (IProtein protein : this.proteinList)
				{
					String proteinSequence = protein.getProteinSequence();

					if (proteinSequence == null || "".equals(proteinSequence.trim()))
					{
						addErrorMessage(Constants.FORM_VALIDATION_PROTEIN_SEQUENCE_INCORRECT2.replace("{0}", protein.getProteinID()));
					}
					else
					{
						if (!UtilAminoAcids.getInstance().validateProteinSequence(proteinSequence))
						{
							addErrorMessage(Constants.FORM_VALIDATION_PROTEIN_SEQUENCE_INCORRECT2.replace("{0}", protein.getProteinID()));
						}
					}

				}
			}
		}
	}

	public PredictionApproach getPredictionApproach()
	{
		return predictionApproach;
	}

	public void setPredictionApproach(String approach) throws Exception
	{
		this.predictionApproach = new PredictionApproach(approach);
	}

	public String getProteinsSequence()
	{
		return proteinsSequence;
	}

	public void setProteinsSequence(String proteinsSequence)
	{
		this.proteinsSequence = proteinsSequence;
	}

	public void addErrorMessage(String errorMessage)
	{
		this.errorMessages.add(errorMessage);
	}

	public double getSpecificity()
	{
		return specificity;
	}

	public void setSpecificity(double specificity)
	{
		this.specificity = specificity;
	}

	public String getEmail()
	{
		return email;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}

	public ArrayList<IProtein> getProteinList()
	{
		return proteinList;
	}
	
}
